package com.tancire.net.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.core.domain.entity.SysUser;
import com.tancire.common.enums.WithdrawStatus;
import com.tancire.common.exception.net.NetActivityException;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.common.utils.StringUtils;
import com.tancire.net.domain.CrmCompany;
import com.tancire.net.domain.CrmCompanyPurse;
import com.tancire.net.service.ICrmCompanyPurseService;
import com.tancire.net.service.ICrmCompanyService;
import org.springframework.stereotype.Service;
import com.tancire.net.mapper.CrmCompanyWithdrawMapper;
import com.tancire.net.domain.CrmCompanyWithdraw;
import com.tancire.net.service.ICrmCompanyWithdrawService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户提现记录Service业务层处理
 *
 * @author chencho
 * @date 2023-12-25
 */
@Service
public class CrmCompanyWithdrawServiceImpl implements ICrmCompanyWithdrawService {

    @Resource
    private CrmCompanyWithdrawMapper crmCompanyWithdrawMapper;

    @Resource
    private ICrmCompanyPurseService crmCompanyPurseService;

    @Resource
    private ICrmCompanyService crmCompanyService;

    /**
     * 查询客户提现记录
     *
     * @param id 客户提现记录主键
     * @return 客户提现记录
     */
    @Override
    public CrmCompanyWithdraw selectCrmCompanyWithdrawById(Long id) {
        return crmCompanyWithdrawMapper.selectCrmCompanyWithdrawById(id);
    }

    /**
     * 查询客户提现记录列表
     *
     * @param crmCompanyWithdraw 客户提现记录
     * @return 客户提现记录
     */
    @Override
    public List<CrmCompanyWithdraw> selectCrmCompanyWithdrawList(CrmCompanyWithdraw crmCompanyWithdraw) {
        return crmCompanyWithdrawMapper.selectCrmCompanyWithdrawList(crmCompanyWithdraw);
    }

    /**
     * 新增客户提现记录
     *
     * @param crmCompanyWithdraw 客户提现记录
     * @return 结果
     */
    @Override
    public int insertCrmCompanyWithdraw(CrmCompanyWithdraw crmCompanyWithdraw) {
        crmCompanyWithdraw.setCreateTime(DateUtils.getNowDate());
        crmCompanyWithdraw.setId(IdUtil.getSnowflakeNextId());
        return crmCompanyWithdrawMapper.insertCrmCompanyWithdraw(crmCompanyWithdraw);
    }

    /**
     * 修改客户提现记录
     *
     * @param crmCompanyWithdraw 客户提现记录
     * @return 结果
     */
    @Override
    public int updateCrmCompanyWithdraw(CrmCompanyWithdraw crmCompanyWithdraw) {
        crmCompanyWithdraw.setUpdateTime(DateUtils.getNowDate());
        return crmCompanyWithdrawMapper.updateCrmCompanyWithdraw(crmCompanyWithdraw);
    }

    /**
     * 批量删除客户提现记录
     *
     * @param ids 需要删除的客户提现记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmCompanyWithdrawByIds(Long[] ids) {
        return crmCompanyWithdrawMapper.deleteCrmCompanyWithdrawByIds(ids);
    }

    /**
     * 删除客户提现记录信息
     *
     * @param id 客户提现记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmCompanyWithdrawById(Long id) {
        return crmCompanyWithdrawMapper.deleteCrmCompanyWithdrawById(id);
    }

    /**
     * 申请提现
     *
     * @param crmCompanyWithdraw
     * @return
     */
    @Transactional
    @Override
    public int applyWithdraw(CrmCompanyWithdraw crmCompanyWithdraw) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        CrmCompanyPurse crmCompanyPurse = this.crmCompanyPurseService.selectCrmCompanyPurseByCompanyId(user.getUserId());
        BigDecimal withdrawPrice = crmCompanyPurse.getWithdrawPrice();
        if (null == withdrawPrice || withdrawPrice.compareTo(new BigDecimal("100")) < 0) {
            throw new NetActivityException("申请提现失败，可提现金额不足100元");
        }
        // 校验财务信息
        CrmCompany company = this.crmCompanyService.selectCrmCompanyById(user.getUserId());
        String bankAccount = company.getBankAccount();
        if (StringUtils.isBlank(bankAccount)) {
            throw new NetActivityException("请先在设置页完善财务信息");
        }
        Integer miniAppStatus = company.getMiniAppStatus();
        if (null == miniAppStatus || miniAppStatus != 1) {
            throw new NetActivityException("请先在设置页绑定小程序");
        }
        // 校验是否存在提现记录
        CrmCompanyWithdraw companyWithdrawQuery = new CrmCompanyWithdraw();
        companyWithdrawQuery.setCompanyId(user.getUserId());
        companyWithdrawQuery.setWithdrawStatus(WithdrawStatus.PENDING_APPROVAL.getCode());
        List<CrmCompanyWithdraw> crmCompanyWithdraws = this.crmCompanyWithdrawMapper.selectCrmCompanyWithdrawList(companyWithdrawQuery);
        if (ObjectUtil.isNotEmpty(crmCompanyWithdraws)) {
            throw new NetActivityException("申请提现失败，有待审核的提现记录");
        }
        // 构建提现记录
        CrmCompanyWithdraw withdraw = new CrmCompanyWithdraw();
        withdraw.setId(IdUtil.getSnowflakeNextId());
        withdraw.setCompanyId(user.getUserId());
        withdraw.setCompanyName(user.getNickName());
        withdraw.setWithdrawAmount(crmCompanyPurse.getWithdrawPrice());
        withdraw.setWithdrawStatus(WithdrawStatus.PENDING_APPROVAL.getCode());
        withdraw.setCreateTime(new Date());
        int i = this.crmCompanyWithdrawMapper.insertCrmCompanyWithdraw(withdraw);
        // 更新可提现金额
        CrmCompanyPurse upData = new CrmCompanyPurse();
        upData.setId(crmCompanyPurse.getId());
        upData.setWithdrawPrice(BigDecimal.ZERO);
        this.crmCompanyPurseService.updateWithdrawPrice(upData);
        return i;
    }

    /**
     * 审批通过逻辑
     *
     * @param id
     */
    @Override
    public void approvePass(Long id) {
        CrmCompanyWithdraw companyWithdraw = new CrmCompanyWithdraw();
        companyWithdraw.setId(id);
        // 标记为待打款
        companyWithdraw.setWithdrawStatus(WithdrawStatus.PENDING_PAY.getCode());
        // 标记审核通过时间
        companyWithdraw.setProcessTime(new Date());
        // 发送审核成功消息 暂时不加，后期优化
        this.crmCompanyWithdrawMapper.updateCrmCompanyWithdraw(companyWithdraw);
    }

    /**
     * 审批拒绝逻辑
     *
     * @param id
     */
    @Override
    public void approveRefuse(Long id) {
        CrmCompanyWithdraw companyWithdraw = new CrmCompanyWithdraw();
        companyWithdraw.setId(id);
        // 标记为待打款
        companyWithdraw.setWithdrawStatus(WithdrawStatus.REFUSE.getCode());
        companyWithdraw.setRefuseReason("收益数据异常");
        // 发送审核失败消息 暂时不加，后期优化
        this.crmCompanyWithdrawMapper.updateCrmCompanyWithdraw(companyWithdraw);
    }

    /**
     * 标记打款
     *
     * @param crmCompanyWithdraw
     * @return
     */
    @Override
    public int payed(CrmCompanyWithdraw crmCompanyWithdraw) {
        CrmCompanyWithdraw companyWithdraw = new CrmCompanyWithdraw();
        companyWithdraw.setId(crmCompanyWithdraw.getId());
        // 标记为待打款
        companyWithdraw.setWithdrawStatus(WithdrawStatus.PAYED.getCode());
        companyWithdraw.setPayTime(new Date());
        return this.crmCompanyWithdrawMapper.updateCrmCompanyWithdraw(companyWithdraw);
    }
}
